package com.tiwi.gmbdr;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class LevelActivity extends Activity{
	private static ArrayAdapter<Level> listAdapter;
	private Activity activity ;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	
		this.activity = this;
        
        //set fullscreen
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                                WindowManager.LayoutParams.FLAG_FULLSCREEN);
		
		
		List<Level> list = null;
		Level level = new Level();
		list = level.loadList();
		setContentView(R.layout.main_list);

		
		listAdapter = new LevelArrayAdapter(LevelActivity.this, R.layout.custom_list, list);
		
		ListView listView =  (ListView) findViewById(R.id.list);
		listView.setAdapter(listAdapter);
		
		
		ListView lv = listView;
		lv.setTextFilterEnabled(true);
		
		lv.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view, int position,
					long id) {
				
				if(position>0){

				
				
				LoadSoalTask task = new LoadSoalTask(activity);
		    	task.execute();
				}
			
			}
		});
		
		
	}
	
	class Level{
		
		private String labelLevel="";
		private int numberLevel=0;
		
		public Level(String level, int numLevel){
			this.labelLevel = level; 
			this.numberLevel = numLevel;
		}
		
		public Level(){
			
		}
		
		public List<Level> loadList() {
			
			List<Level> result = null ;
			Level bean = null;
			if (result == null)
				result = new ArrayList<Level>();
			bean= new Level("Level 1",1);
			result.add(bean);	
			bean= new Level("Level 2",2);
			result.add(bean);	
			bean= new Level("Level 3",3);
			result.add(bean);
			bean= new Level("Level 4",4);
			result.add(bean);
			
			return result;
		}
		
		public String getLabelLevel(){
			return labelLevel;
		}
		
		public int getNumberLevel(){
			return numberLevel;
		}
		
	}
	
	class LevelArrayAdapter extends ArrayAdapter<Level> {
		private final Activity context;
		private List<Level> objects; 
		private int textViewResourceId;
		private int[] colors = new int[] { 0xAAf6ffc8, 0xAA538d00 };
		public LevelArrayAdapter(Activity context, int textViewResourceId,
				List<Level> objects) {
			super(context, textViewResourceId, objects);
			this.context = context;
			this.objects = objects;
			this.textViewResourceId=textViewResourceId;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			
			View rowView = convertView;
			if (null == rowView) {
				LayoutInflater inflater = context.getLayoutInflater();
				
				 // Have the system blur any windows behind this one.
				context.getWindow().setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND,
		                WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
				
				
				
				rowView = inflater.inflate(textViewResourceId, null, true);
				//Set the background color depending of  odd/even colorPos result
			    int colorPos = position % colors.length;
			  //  rowView.setBackgroundColor(colors[colorPos]);
				
			    
			    holder = new ViewHolder();
				
				
				
				holder.img = (ImageView) rowView.findViewById(R.id.listIcon);
				holder.title = (TextView) rowView.findViewById(R.id.listTitle); 
				//holder.description = (TextView) rowView.findViewById(R.id.listDescription);
				
				rowView.setTag(holder);
			}
			else {
				holder = (ViewHolder) rowView.getTag();
			}
			
			Level bean = objects.get(position);
			//String status = bean.getSentStatus();
			
			holder.title.setText(bean.getLabelLevel());
			//holder.description.setText(bean.toDescription());
			
			int resId = -1;
			resId = R.drawable.note;
			holder.img.setImageResource(resId);
			
			return rowView;
		}

		private class ViewHolder {
			public ImageView img;
			public TextView title;
			//public TextView description;
		}
	}
	
}
